package exer;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * @author tongchen
 * @create 2023-04-01 17:46
 */
@WebServlet("/jackson")
public class JacksonTest extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求数据的编码集
        req.setCharacterEncoding("utf-8");
        //设置响应数据的格式

        resp.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(req.getInputStream(), Student.class);
        student.setStudentId(100);
        student.setStudentName("hihihi");
        //发回数据
        String s = objectMapper.writeValueAsString(student);
        System.out.println(s);
        resp.getWriter().write(s);

    }
    //设置请求和响应格式

}
